#include "CH58x_common.h"
#include "stdio.h"
#include "DevicesTime.h"
#include "DevicesQueue.h"
#include "microConfig.h"
#include "taskSystem.h"


void UART0_IRQHandler(void) __attribute__((interrupt())) __attribute__((section(".highcode")));
void UART0_IRQHandler(void)
{
    vMrStackSwitchToSystem();

    switch(UART0_GetITFlag())
    {
        /* 串口0独有 */
        case UART_II_MODEM_CHG: (void)R8_UART0_MSR; break;
        case UART_II_LINE_STAT: UART0_GetLinSTA();  break;
        case UART_II_THR_EMPTY:                     break;

        case UART_II_RECV_RDY:
        case UART_II_RECV_TOUT:
            while(R8_UART0_RFC)
            {
                enumQueuePushByte(&g_TypeQueueUart0Read, UART0_RecvByte());
            }

            /* 触发任务执行 */
            cMrMessageSemaphoreSendIsr(g_ptypeMrMessageNetwork);
            break;

        default: break;
    }

    vMrStackSwitchToTask();
}

void UART1_IRQHandler(void) __attribute__((interrupt()));
void UART1_IRQHandler(void)
{
    vMrStackSwitchToSystem();

    switch(UART1_GetITFlag())
    {
        case UART_II_LINE_STAT: UART1_GetLinSTA();  break;
        case UART_II_THR_EMPTY:                     break;

        case UART_II_RECV_RDY:
        case UART_II_RECV_TOUT:
            while(R8_UART1_RFC)
            {
                enumQueuePushByte(&g_TypeQueueUart1Read, UART1_RecvByte());
            }
            break;

        default: break;
    }

    vMrStackSwitchToTask();
}

void UART2_IRQHandler(void) __attribute__((interrupt()));
void UART2_IRQHandler(void)
{
    vMrStackSwitchToSystem();

    switch(UART2_GetITFlag())
    {
        case UART_II_LINE_STAT: UART2_GetLinSTA();  break;
        case UART_II_THR_EMPTY:                     break;

        case UART_II_RECV_RDY:
        case UART_II_RECV_TOUT:
            while(R8_UART2_RFC)
            {
                enumQueuePushByte(&g_TypeQueueUart2Read, UART2_RecvByte());
            }
            break;

        default: break;
    }

    vMrStackSwitchToTask();
}

void UART3_IRQHandler(void) __attribute__((interrupt()));
void UART3_IRQHandler(void)
{
    vMrStackSwitchToSystem();

    switch(UART3_GetITFlag())
    {
        case UART_II_LINE_STAT: UART3_GetLinSTA();  break;
        case UART_II_THR_EMPTY:                     break;

        case UART_II_RECV_RDY:
        case UART_II_RECV_TOUT:
            while(R8_UART3_RFC)
            {
                enumQueuePushByte(&g_TypeQueueUart3Read, UART3_RecvByte());
            }
            break;

        default: break;
    }

    vMrStackSwitchToTask();
}

void SysTick_Handler(void) __attribute__((interrupt()));
void SysTick_Handler(void)
{
    SysTick->SR = 0;

    extern void vMrCpuSysTickHandler(void);
    vMrCpuSysTickHandler();
}

void TMR3_IRQHandler(void) __attribute__((interrupt()));
void TMR3_IRQHandler(void)
{
    if(TMR3_GetITFlag(TMR0_3_IT_CYC_END))
    {
        TMR3_ClearITFlag(TMR0_3_IT_CYC_END);

        g_lTimeBase += 1 << 26;
    }
}
